package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.ProductTypeDto;
import cn.chinaflame.pagodas.req.YwProductTypeReq;
import cn.chinaflame.pagodas.service.YwProductTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 商品相关接口
 *
 * @author admin
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "openapi/v1/pagodas/product/type/")
public class OpenProductTypeAPI {
    private static Logger log = LoggerFactory.getLogger(OpenProductTypeAPI.class);
    @Autowired
    private YwProductTypeService productTypeService;

    /**
     * 商品类别添加
     *
     * @param data={"name":"name","itemId":"itemId"}
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(Result.class)
    public Object add(@RequestBody Map<String, Object> map) {
        log.info("商品类别添加接口map:{}", map);
        ProductTypeDto rs = productTypeService.add(map);
        return Result.create(rs);
    }

    /**
     * 商品类别修改
     *
     * @param data={"id":"id","name":"name","itemId":"itemId"}
     * @return
     */
    @PutMapping(value = "update")
    @ApiDoc(Result.class)
    public Object update(String data) {
        log.info("商品类别修改接口data:{}", data);
        ProductTypeDto rs = productTypeService.update(data);
        return Result.create(rs);
    }

    /**
     * 商品类别删除
     *
     * @param id 商品id
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ApiDoc(Result.class)
    public Object delete(@PathVariable("id") String id) {
        log.info("商品类别删除接口data:{}", id);
        boolean rs = productTypeService.delete2(id);
        return Result.create(rs);
    }


    /**
     * 商品类别多条件查询
     *
     * @param pageIndex 页码
     * @param pageSize  页数
     * @param id        商品类别ID
     * @param name      商品类别名称
     * @param itemId    项目ID
     * @return
     */
    @PostMapping(value = "query/list")
    @ApiDoc(Result.class)
    public Object queryList(@RequestBody YwProductTypeReq req) {
        log.info("商品类别查询【service】req:{}", req);
        return productTypeService.queryByPage(req);
    }

    /**
     * 商品类别查询
     *
     * @param id 商品id
     * @return
     */
    @GetMapping(value = "query")
    @ApiDoc(Result.class)
    public Object query(String id) {
        log.info("商品类别查询接口id:{}", id);
        List<ProductTypeDto> rs = productTypeService.query(null, null, id, null, null, null);
        ProductTypeDto pd = rs.get(0);
        return Result.create(pd);
    }


}
